package com.yusys.jin.blog.configuration;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {
    @Value("${allowedOrigins}")
    private String url;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        HandlerInterceptor interceptor = new LoginIntercepteor();

        List<String> list = new ArrayList<String>();
        list.add("/html/index.html");
        list.add("/html/login.html");
        list.add("/css/**");
        list.add("/js/**");
        list.add("/images/**");
        list.add("/bootstrap-3.3.7-dist/**");


        list.add("/public/**");
        list.add("/users/reg");
        list.add("/users/checkusername");
        list.add("/users/login");


        registry.addInterceptor(interceptor).addPathPatterns("/**")
                .excludePathPatterns(list);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins(url)
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }

}
